#
# Author :: Madhu CK <madhu_c_k@us.ibm.com>
# Cookbook Name: was
# Definition: registerprofile
#
# Copyright 2013, IBM Corporation
#
#

define :registerprofile do
  profilename = params[:name]
  zkep = "#{node[:zookeeper][:ip]}:#{node[:zookeeper][:port]}"
  
  chef_gem 'zk'
    
  # Extract the portdefs default, websecure, soapport for the profile
  ruby_block "Extract port information for profile #{profilename}" do
    block do
      portdeffile = "#{node[:was][:installDir]}/profiles/#{profilename}/properties/portdef.props"
      points = {}
      %w(SOAP_CONNECTOR_ADDRESS WC_defaulthost_secure WC_defaulthost WC_adminhost_secure).each do |portname|
        portline = `grep #{portname}= #{portdeffile}`
        port = /\=(.*)$/.match(portline)[1]
        points[portname] = port
      end
      points[:fqdn] = node[:fqdn]
      status = {:ready => true}
      
      require 'zk'
      require 'json'
      i_statusurl = node[:was][:endpointstatus]
      i_infourl = node[:was][:endpointinfo]
      begin
        z = ZK.new(zkep)
        z.create(i_infourl, points.to_json, {:or => :set})
        z.create(i_statusurl, status.to_json, {:or => :set})
      ensure
        z.close
      end
    end
  end
end